package com.hdoit.ares.web.onlineSer.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.official.model.Artical;
import com.hdoit.ares.logical.official.model.ArticalClassify;
import com.hdoit.ares.logical.official.service.IArticalClassifyService;
import com.hdoit.ares.logical.official.service.IArticalService;
import com.hdoit.ares.logical.viewmodel.official.ArticalClassifySearch;
import com.tennetcn.common.logical.file.model.OssFileInfo;
import com.tennetcn.common.logical.file.service.IOssFileInfoService;
import com.tennetcn.component.webapi.BaseApi;
import com.tennetcn.component.webapi.ResponseModel;
import com.tennetcn.component.webapi.ResponseStatus;
import com.tennetcn.web.cbase.ApiAuthPassport;

@Controller
@RequestMapping(value="/api/v1/onlineSer/",method = {RequestMethod.GET,RequestMethod.POST},produces="application/json;charset=utf-8")
@Api(value="/api/v1/onlineSer/")
@CrossOrigin(origins = "*",maxAge = 3600)
public class onlineSerAresApi extends BaseApi{

	private IArticalClassifyService articalClassifyService;
	public IArticalClassifyService getArticalClassifyService() {
		return articalClassifyService;
	}
	@Resource
	public void setArticalClassifyService(IArticalClassifyService articalClassifyService) {
		this.articalClassifyService = articalClassifyService;
	}

	private IArticalService articalService;
	public IArticalService getArticalService() {
		return articalService;
	}
	@Resource
	public void setArticalService(IArticalService articalService) {
		this.articalService = articalService;
	} 
	
	private IOssFileInfoService fileInfoService;
	public IOssFileInfoService getFileInfoService() {
		return fileInfoService;
	}
	@Resource
	public void setFileInfoService(IOssFileInfoService fileInfoService) {
		this.fileInfoService = fileInfoService;
	}

	private final String onlineSerApi="onlineSer";
	private final String onlineSerDetailApi="onlineSerDetail";
	private final String onlineQuestionApi="onlineQuestion";
	
	@ApiAuthPassport
	@ResponseBody
	@RequestMapping(value = onlineSerApi)
	@ApiOperation(notes = onlineSerApi, httpMethod = "POST", value = "查询类型为菜单父级为在线客服的所有列表")
	public String onlineSer(){
		 
		ResponseModel rm=new ResponseModel();
//		查询类型为菜单父级为在线客服的所有列表
		String type="menu";
		String url="areas/wechat/onlineSer/onlineSer";
		ArticalClassifySearch search=new ArticalClassifySearch();
		search.setFunctionURL(url);
		ArticalClassify articalClassify=articalClassifyService.queryModel(search);
		String parentId=null;
		if(articalClassify!=null){ 
			 parentId=articalClassify.getId();
		}
		/*真实的图片路径前缀*/
		String preUrl = "http://putiyoli-1254043822.cosgz.myqcloud.com";
		
		/*测试的图片路径前缀*/
		/*String preUrl = "http://test-1254043822.cosgz.myqcloud.com";*/
		
		List<ArticalClassify> articalClassifys=articalClassifyService.queryOnlineSerList(type, parentId);
		if(articalClassifys.size()>0){
			rm.setStatus(ResponseStatus.success);
			rm.setMessage("ok");
			for(ArticalClassify articalClassify2:articalClassifys){
				if(!StringUtils.isEmpty(articalClassify2.getImgURL())){
					OssFileInfo ossFileInfo = fileInfoService.queryModel(articalClassify2.getImgURL());
					articalClassify2.setImgURL(preUrl+ossFileInfo.getFilename());
				}
			}
			List<String> params = new ArrayList<String>();
			Field[] fields = ArticalClassify.class.getDeclaredFields();
			for(Field field : fields){
				params.add(field.getName());
			}
			
			rm.putList("articalClassify2", articalClassifys, params);
		}else{
			rm.setMessage("没有查询到数据");
		}
		 
		return rm.toJson();
	}
	
	@ApiAuthPassport
	@ResponseBody
	@RequestMapping(value = onlineSerDetailApi)
	@ApiOperation(notes = onlineSerDetailApi, httpMethod = "POST", value = "查询选择大标题下的所有小标题")
	public String onlineSerDetail(@ApiParam("选择的标题类型typeId,必选") @RequestParam(value="typeId",required=true)String typeId){
		 
		ResponseModel rm=new ResponseModel();

		List<Artical> articals=articalService.queryListArtical(typeId);
		if(articals.size()>0){
			rm.setStatus(ResponseStatus.success);
			rm.setMessage("ok");
			List<String> params = new ArrayList<String>();
			Field[] fields = Artical.class.getDeclaredFields();
			for(Field field : fields){
				params.add(field.getName());
			}
			
			rm.putList("artical", articals, params);
		}else{
			rm.setMessage("没有查询到数据");
		}
		 
		return rm.toJson();
	}
	
	@ApiAuthPassport
	@ResponseBody
	@RequestMapping(value = onlineQuestionApi)
	@ApiOperation(notes = onlineQuestionApi, httpMethod = "POST", value = "查询具体问题")
	public String onlineQuestion(@ApiParam("选择的问题id,必选") @RequestParam(value="id",required=true)String id){
		 
		/*真实的图片路径前缀*/
		String preUrl = "http://putiyoli-1254043822.cosgz.myqcloud.com";
		
		/*测试的图片路径前缀*/
		/*String preUrl = "http://test-1254043822.cosgz.myqcloud.com";*/
		
		ResponseModel rm=new ResponseModel();

		Artical artical=articalService.queryArticalById(id);
		
		if(!StringUtils.isEmpty(artical)){
			rm.setStatus(ResponseStatus.success);
			rm.setMessage("ok");
			if(!StringUtils.isEmpty(artical.getImgUrl())){
				OssFileInfo ossFileInfo = fileInfoService.queryModel(artical.getImgUrl());
				artical.setImgUrl(preUrl+ossFileInfo.getFilename());
			}
			if(!StringUtils.isEmpty(artical.getVideoUrl())){
				OssFileInfo ossFileInfo = fileInfoService.queryModel(artical.getVideoUrl());
				artical.setVideoUrl(preUrl+ossFileInfo.getFilename());
			}
			
			rm.put("artical", artical);
		}else{
			rm.setMessage("没有查询到内容");
		}
		 
		return rm.toJson();
	}
}
